----------------------------------------------------------------------------------
-- Company: 
-- Engineer: 
-- 
-- Create Date:    15:12:51 05/26/2010 
-- Design Name: 
-- Module Name:    Trigger_Module - Behavioral 
-- Project Name: 
-- Target Devices: 
-- Tool versions: 
-- Description: 
--
-- Dependencies: 
--
-- Revision: 
-- Revision 0.01 - File Created
-- Additional Comments: 
--
----------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;

---- Uncomment the following library declaration if instantiating
---- any Xilinx primitives in this code.
--library UNISIM;
--use UNISIM.VComponents.all;

entity Trigger_Module is
    Port ( Data_A_in : in  STD_LOGIC_VECTOR (11 downto 0);
           Data_B_in : in  STD_LOGIC_VECTOR (11 downto 0);
           Data_A_out : out  STD_LOGIC_VECTOR (11 downto 0);
           Data_B_out : out  STD_LOGIC_VECTOR (11 downto 0);
           Trigger_out : out  STD_LOGIC;
           Trigger_level_in : in  STD_LOGIC_VECTOR (11 downto 0);
           Trigger_A_B : in  STD_LOGIC;
			  Trigger_Edge : in STD_LOGIC;
			  Trigger_Mode : in STD_LOGIC;
			  reset_in : in STD_LOGIC;
			  newdata_in :in STD_LOGIC;
			  newdata_out : out STD_LOGIC := '0');
end Trigger_Module;

architecture Behavioral of Trigger_Module is


signal Data_old : STD_LOGIC_VECTOR (11 downto 0) := "000000000000";

begin

P1: process(newdata_in, reset_in)
begin

if rising_edge(newdata_in) then
	Data_A_out <= Data_A_in;
	Data_B_out <= Data_B_in;
	if Trigger_A_B = '1' then  --Channel A Trigger
		if Trigger_Edge = '1' then --Rising Edge
			if (Data_old < Trigger_level_in) and (Data_A_in >= Trigger_level_in) then
				Trigger_out <= '1';
			else
				Trigger_out <= '0';
			end if;
		else
			if (Data_old >= Trigger_level_in) and (Data_A_in < Trigger_level_in) then
				Trigger_out <= '1';
			else
				Trigger_out <= '0';
			end if;
		end if;
		Data_old <= Data_A_in;
	else --Trigger_A_B = '0' then
		if Trigger_Edge = '1' then --Rising Edge
				if (Data_old < Trigger_level_in) and (Data_B_in >= Trigger_level_in) then
					Trigger_out <= '1';
				else
					Trigger_out <= '0';
				end if;
			else
				if (Data_old >= Trigger_level_in) and (Data_B_in < Trigger_level_in) then
					Trigger_out <= '1';
				else
					Trigger_out <= '0';
				end if;
		end if;
			Data_old <= Data_B_in;
	end if;
end if;	

if reset_in = '1' then
	Data_old <= "000000000000";
	Trigger_out <= '0';
end if;

newdata_out <= newdata_in;
end process;

end Behavioral;

